package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author zhangxiaosheng
 * @date 2020/9/4 - 10:25
 */
@RestController
@RequestMapping ("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    /**
     * 查找所有父菜单信息
     *
     * @return
     */
    @GetMapping ("/findParentMenu")
    public Result findParentMenu() {
        List<Menu> menuList = menuService.findParentMenu();
        return new Result(true, MessageConstant.QUERY_PARENTMENU_SUCCESS, menuList);
    }

    /**
     * 添加菜单
     *
     * @param menu
     * @return
     */
    @PostMapping("/add")
    public Result addMenu(@RequestBody Menu menu,Integer[] parentMenuIds) {
        menuService.addMenu(menu,parentMenuIds);
        return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult<Menu> pageResult = menuService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, pageResult);
    }

    /**
     * 删除菜单
     * @return
     */
    @PostMapping("/deleteById")
    public Result deleteMenu(int id){
        menuService.deleteMenu(id);
        return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
    }

    /**
     * 回显菜单
     * @param id
     * @return
     */
    @PostMapping("/findById")
    public Result findById(int id){
        Menu menu=menuService.findById(id);
        return new Result(true,MessageConstant.SHOW_MENU_SUCCESS,menu);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<Menu> menuList = menuService.findAll();
        return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menuList);
    }

    /**
     * 查询父菜单id
     * @param id
     * @return
     */
    @PostMapping("/findParentId")
    public Result findParentId(int id){
        Integer parentId = menuService.findParentId(id);
        return new Result(true,MessageConstant.SHOW_MENU_SUCCESS,parentId);
    }

    /**
     * 编辑菜单
     * @param menu
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Menu menu){
        menuService.update(menu);
        return new Result(true,MessageConstant.UPDATE_MENU_SUCCESS);
    }
}
